var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookdefault: app.myUlife.ulifeUrl + 'testimage/timg1.jpg',//默认图片 
    bid: '',//书籍ID
    bookdeatil: {},//书籍详情
    usid: '',
    sc: 0,//是否收藏 0否 
    sj: 0,//是否加入书架 0否 

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    this.setData({
      usid: usid
    })
    //获取传递的参数
    this.setData({
      bid: options.bid
    })
    this.getBookDetail();

    //查询是否收藏 是否加入书架
    this.getSfscJrsj();

  },


  getBookDetail() {
    var that = this;
    var bid = that.data.bid;
    var usid = wx.getStorageSync('usid');
    app.wxget({
      url: 'wbookdetail/getBookDetail.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        var bookdeatil = res.data;
        that.setData({
          bookdeatil: bookdeatil
        })

      }
    })
  },


  //查询是否收藏  是否加入书架
  getSfscJrsj() {
    var that = this;
    var bid = that.data.bid;
    var usid = that.data.usid;
    app.wxget({
      url: 'wbookdetail/getSfscJrsj.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        that.setData({
          sc: res.data.SC,
          sj: res.data.SJ,
        })
      }
    })
  },

  errorFm: function (e) {
    if (e.type == "error") {
      var bookdeatil = this.data.bookdeatil    　　　　　　　//将图片列表数据绑定到变量
      bookdeatil.FM = this.data.bookdefault //错误图片替换为默认图片
      this.setData({
        bookdeatil: bookdeatil
      })
    }
  },

  //我要收藏
  collectionbook(e) {
    var that = this;
    var bid = that.data.bid;
    var usid = that.data.usid;
    app.wxget({
      url: 'wbookdetail/collectionbook.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        if (res.data) {
          that.setData({
            sc: 1,
          })
          wx.showModal({
            title: '成功',
            content: '收藏成功!',
            showCancel: !1,
          })
        } else {
          wx.showModal({
            title: '友情提示',
            content: '收藏失败!',
            showCancel: !1,
          })
        }
      }
    })
  },


  //取消收藏
  cancelcollectionbook(e) {
    var that = this;
    var bid = that.data.bid;
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/index/index'
      })
      return;
    }
    app.wxget({
      url: 'wbookdetail/cancelcollectionbook.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        if (res.data) {
          that.setData({
            sc: 0,
          })
          wx.showModal({
            title: '成功',
            content: '取消成功!',
            showCancel: !1,
          })
        } else {
          wx.showModal({
            title: '友情提示',
            content: '取消失败!',
            showCancel: !1,
          })
        }
      }
    })
  },


})